﻿using System.Windows.Controls;
using Microsoft.VisualStudio.Text.Editor;
using Sentio.CodeReview.Foundation;
using System.Threading;


namespace Sentio.CodeReview.Editor
{
    public class CodeReviewStatusControlMargin : Canvas, IWpfTextViewMargin
    {
        private bool _disposed = false;
        private readonly string _filePath;
        private readonly SolutionConfiguration _svnConfig;
        private readonly CodeReviewStatusControlBarUI _control;

        public CodeReviewStatusControlMargin(string filePath)
        {
            _filePath = filePath;
            _control = new CodeReviewStatusControlBarUI();

            _svnConfig = new SolutionConfiguration()
            {
                SvnProjectRoot = "https://svn-code-review.googlecode.com/svn/trunk/Sentio.CodeReview",
                Username = "karim.dev",
                SolutionBaseDirectory = @"C:\Users\Filip\Documents\Sentio.CodeReview\"
            };
            
            ThreadPool.QueueUserWorkItem(delegate
            {
                SvnTargetRepository repository = new SvnTargetRepository(_svnConfig);
                Target fileTarget = repository.GetTarget(filePath.Replace(_svnConfig.SolutionBaseDirectory, ""));
                _control.UpdateCommentsCount(fileTarget.Comments.Count, fileTarget.RelevantComments.Count);
            });

            this.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            this.Margin = new System.Windows.Thickness(0, 0, 268, 0);
            this.Children.Add(_control);
        }

        public System.Windows.FrameworkElement VisualElement
        {
            get
            {
                if (!_disposed)
                    return this;
                return null;
            }
        }

        public bool Enabled
        {
            get
            {
                return !_disposed;
            }
        }

        public ITextViewMargin GetTextViewMargin(string marginName)
        {
            return (marginName == CodeReviewStatusControlBar.MarginName) ? (IWpfTextViewMargin)this : null;
        }

        public double MarginSize
        {
            get { return this.ActualHeight; }
        }

        public void Dispose()
        {
            if (!_disposed)
            {
                _disposed = true;
            }
        }
    }
}
